'use strict';

angular.module('ProduceWork').controller('MOOnlineController', ['$scope', '$resource', 'EntityService', 'BaseModule', 'BaseEnum', 'BaseFunction', 'BarcodeEnum', '$location', 'usbPort', 'Authentication',
    function($scope, $resource, EntityService, BaseModule, BaseEnum, BaseFunction, BarcodeEnum, $location, usbPort, Authentication) {
        $scope.MOStatusEnum = BaseFunction.getEnum('ProduceReadyEnum.MOStatus');

        BaseModule.initModule($scope, 'MO');
        $scope.Module.MO.LoadMode = BaseEnum.LoadMode.CacheAndPaging;
        $scope.Module.MO.Condition = {
            $where : "this.OnlineQty == null || this.OnlineQty < this.Qty",
            Status: {$nin: [$scope.MOStatusEnum.Downline.Value, $scope.MOStatusEnum.Complete.Value]}
        };
        $scope.Module.MO.Populate = [{path: 'ItemMaster', select: 'Code Name'}];
        $scope.Module.MO.PageSize = 15;

        BaseModule.initModule($scope, 'BarcodeMaster');
        $scope.Module.BarcodeMaster.LoadMode = BaseEnum.LoadMode.CacheAndNoPaging;
        $scope.Module.BarcodeMaster.PageSize = 50;
        $scope.Module.BarcodeMaster.OrderBy = {OrderNum: 1};

        $scope.btnOnLine = function(){
            $scope.Module.MO.FocusedRecord.currentQty = 0;
            $scope.Module.BarcodeMaster.Records = [];
            $scope.Module.BarcodeMaster.emitReloadEvent();
            usbPort.start();
        };
        $scope.onModalHide = function () {
            usbPort.stop();
        };
        $scope.$on('usbPort.input', function (e, inputStr) {
            $scope.addChipRow(inputStr);
        });

        $scope.addChipRow = function(hexID){
            if ($scope.Module.MO.FocusedRecord.Qty - $scope.Module.MO.FocusedRecord.OnlineQty <= 0) {
                return;
            }
            var maxLineValue = 0;
            for(var i=0; i<$scope.Module.BarcodeMaster.Records.length; i++){
                if($scope.Module.BarcodeMaster.Records[i].Barcode == hexID){
                    return;
                }

                if($scope.Module.BarcodeMaster.Records[i].OrderNum !== "" &&
                    $scope.Module.BarcodeMaster.Records[i].OrderNum != undefined &&
                    $scope.Module.BarcodeMaster.Records[i].OrderNum > maxLineValue)
                {
                    maxLineValue = $scope.Module.BarcodeMaster.Records[i].OrderNum;
                }
            }
            var newRecord = $scope.Module.BarcodeMaster.add();
            newRecord.OrderNum = maxLineValue + 1;
            newRecord.Barcode = hexID;
            newRecord.Status = BarcodeEnum.BarcodeStatus.Online.Value;
            newRecord.BarcodeType = BarcodeEnum.BarcodeType.ItemMaster.Value;
            newRecord.Status = BarcodeEnum.BarcodeStatus.Online.Value;
            newRecord.MO = $scope.Module.MO.FocusedRecord._id;
            newRecord.MOCode = $scope.Module.MO.FocusedRecord.Code;
            newRecord.ItemMaster = $scope.Module.MO.FocusedRecord.ItemMaster._id;
            newRecord.ItemMasterCode = $scope.Module.MO.FocusedRecord.ItemMaster.Code;
            newRecord.ItemMasterName = $scope.Module.MO.FocusedRecord.ItemMaster.Name;
            newRecord.MOLotNumber = $scope.Module.MO.FocusedRecord.LotNumber;
            newRecord.Specification = $scope.Module.MO.FocusedRecord.ItemMaster.Specification;
            //侨兴定制开发
            if(newRecord.ItemMasterCode.indexOf('-') > 0){
                var itemSplits = newRecord.ItemMasterCode.split('-');
                newRecord.DescSeg1 = itemSplits[itemSplits.length - 1];
            }

            //持久化
            var saveDatas = [];
            saveDatas.push({
                EntityName: 'BarcodeMaster',
                Records: [newRecord]
            });

            if($scope.Module.MO.FocusedRecord.OnlineQty == null
                || $scope.Module.MO.FocusedRecord.OnlineQty == undefined
                || $scope.Module.MO.FocusedRecord.OnlineQty <= 0){
                $scope.Module.MO.FocusedRecord.Status = $scope.MOStatusEnum.Online.Value;
                $scope.Module.MO.FocusedRecord.OnlineQty = 1;
                $scope.Module.MO.FocusedRecord.StartDateTime = new Date();
            }
            else{
                $scope.Module.MO.FocusedRecord.OnlineQty += 1;
            }
            $scope.Module.MO.FocusedRecord.RowStatus = BaseEnum.RowStatus.Modify.Value;
            saveDatas.push({
                EntityName: 'MO',
                Records: [$scope.Module.MO.FocusedRecord]
            });
            EntityService.saves(saveDatas, function(res){
                $scope.Module.BarcodeMaster.emitOnLoadEvent();
                //刷新MO数据
                $scope.Module.MO.findById($scope.Module.MO.FocusedRecord._id, function(record){
                    $scope.Module.MO.FocusedRecord = record;
                    $scope.Module.MO.emitRowsDataChangeEvent([record]);
                });

                BaseFunction.tip('上线成功', '条码编号：' + newRecord.Barcode);
            }, function(err){
                $scope.Module.BarcodeMaster.removeRecord(newRecord);
                $scope.Module.MO.FocusedRecord.OnlineQty -= 1;
                if($scope.Module.MO.FocusedRecord.OnlineQty == 0){
                    $scope.Module.MO.FocusedRecord.StartDateTime = null;
                }
                if(err.message.indexOf("E11000") > -1){
                    BaseFunction.alert("芯片:" + newRecord.Barcode + "已经被使用", '上线失败');
                }
                else{
                    BaseFunction.alert(err.message, err.title);
                }
            });
        };

        $scope.printBarcode = function(){
            if($scope.Module.BarcodeMaster.FocusedRecord != null){
                BaseFunction.alert($scope.Module.BarcodeMaster.FocusedRecord.Barcode, '打印条码');
            }
            angular.element(".modal-body").printThis();
        };
    }
]);
